<template>
  <div>
    <el-card>
      <!--表格-->
      <el-row>
        <el-table :data="orderList" border style="width: 100%">
          <el-table-column type="index" label="#" width="60px">
          </el-table-column>
          <el-table-column
            prop="order_number"
            label="订单编号"
            width="280px"
          ></el-table-column>
          <el-table-column
            prop="order_price"
            label="订单价格"
            width="200px"
          ></el-table-column>
          <el-table-column label="是否支付" width="150px">
            <template v-slot="scope">
              <el-tag type="danger" v-if="scope.row.pay_status === '0'"
                >未付款</el-tag
              >
              <el-tag type="success" v-else>已付款</el-tag>
            </template>
          </el-table-column>
          <el-table-column
            prop="is_send"
            label="是否发货"
            width="100px"
          ></el-table-column>
          <el-table-column label="下单时间" width="250px">
            <template v-slot="scope">
              {{ scope.row.create_time | dateFormat }}
            </template>
          </el-table-column>
          <el-table-column label="操作" width="150px">
            <template slot-scope="scope">
              <el-tooltip
                class="item"
                effect="dark"
                content="修改订单状态"
                placement="top"
              >
                <el-button
                  type="primary"
                  icon="el-icon-edit"
                  circle
                  @click="showEditOrderDialog(scope.row)"
                ></el-button>
              </el-tooltip>
              <el-tooltip
                class="item"
                effect="dark"
                content="查看订单详情"
                placement="top"
              >
                <el-button
                  type="success"
                  icon="el-icon-folder-opened"
                  circle
                  @click="showOrderDetailed(scope.row.order_id)"
                ></el-button>
              </el-tooltip>
            </template>
          </el-table-column>
        </el-table>
      </el-row>
      <!---分页按钮--->
      <el-row>
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="queryInfo.pagenum"
          :page-sizes="[5, 10, 20, 30]"
          :page-size="queryInfo.pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          background
        >
        </el-pagination>
      </el-row>
    </el-card>
    <!-- 修改用户信息弹出框 -->
    <el-dialog title="修改订单" :visible.sync="dialogOrderEdit" width="60%">
      <el-form :model="orderEditData" label-width="100px" class="demo-ruleForm">
        <el-form-item label="订单 ID">
          <el-input
            v-model="orderEditData.order_id"
            :disabled="true"
          ></el-input>
        </el-form-item>
        <el-form-item label="订单是否发货">
          <el-input v-model="orderEditData.is_send" placeholder="填1=>是,其他否"></el-input>
        </el-form-item>
        <el-form-item label="订单支付">
          <el-input v-model="orderEditData.order_pay"></el-input>
        </el-form-item>
        <el-form-item label="订单价格">
          <el-input v-model="orderEditData.order_price"></el-input>
        </el-form-item>
        <el-form-item label="订单编号">
          <el-input v-model="orderEditData.order_number"></el-input>
        </el-form-item>
        <el-form-item label="支付状态">
          <el-input v-model="orderEditData.pay_status" placeholder="填1=>已支付,0=>未支付"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="editOrder">确 定</el-button>
        <el-button @click="editcancel">取 消</el-button>
      </span>
    </el-dialog>
    <!-- 订单详细信息展示框 -->
    <el-dialog title="订单详情" :visible.sync="dialogOrderDetailed" width="60%">
      <el-descriptions :column="2" border>
        <el-descriptions-item label="用户ID">{{
          orderDetailedData.user_id
        }}</el-descriptions-item>
        <el-descriptions-item label="支付状态"><el-tag type="danger" v-if="orderDetailedData.pay_status === '0'"
                >未付款</el-tag
              >
              <el-tag type="success" v-else>已付款</el-tag></el-descriptions-item>
        <el-descriptions-item label="是否发货">{{
          orderDetailedData.is_send
        }}</el-descriptions-item>
        <el-descriptions-item label="创建时间">{{
          orderDetailedData.create_time | dateFormat
        }}</el-descriptions-item>
        <el-descriptions-item label="订单编号">{{
          orderDetailedData.order_number
        }}</el-descriptions-item>
        <el-descriptions-item label="订单价格">{{
          orderDetailedData.order_price
        }}</el-descriptions-item>
        <el-descriptions-item label="['个人'，'公司']">{{
          orderDetailedData.order_fapiao_title
        }}</el-descriptions-item>
        <el-descriptions-item label="公司名称">{{
          orderDetailedData.order_fapiao_company
        }}</el-descriptions-item>
        <el-descriptions-item label="发票内容" labelStyle="width:150px" contentStyle="width:250px">{{
          orderDetailedData.order_fapiao_content
        }}</el-descriptions-item>
        <el-descriptions-item label="发货地址"  labelStyle="width:150px">{{
          orderDetailedData.consignee_addr
        }}</el-descriptions-item>
      </el-descriptions>
        <el-descriptions :column="2" border title="商品信息">
        <template v-for="(item,i) in orderDetailedData.goods" >
          <el-descriptions-item label="商品id" :key="i">{{
          item.goods_id
        }}</el-descriptions-item>
        <el-descriptions-item label="订单id" :key="i">{{
          item.order_id
        }}</el-descriptions-item>
        <el-descriptions-item label="商品数量" :key="i">{{
          item.goods_number
        }}</el-descriptions-item>
        <el-descriptions-item label="商品价格" :key="i">{{
          item.goods_price
        }}</el-descriptions-item>
        <el-descriptions-item label="商品总价" :key="i">{{
          item.goods_id
        }}</el-descriptions-item>
        </template>
      </el-descriptions>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="dialogOrderDetailed = false"
          >关闭</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  //import 引入组件
  components: {},
  data() {
    return {
      queryInfo: {
        query: "",
        pagenum: 1,
        pagesize: 5,
      },
      // 订单列表的数据
      orderList: [],
      // 订单的总数量
      total: 0,
      dialogOrderEdit: false,
      //订单编辑的数据
      orderEditData: {
        is_send:'',
        order_pay:'',
        order_price:'',
        order_number:'',
        pay_status:'',
      },
      //订单详情的数据
      orderDetailedData: {},
      dialogOrderDetailed: false,
    };
  },
  methods: {
    // 获取订单列表数据
    async getOrderList() {
      const { data } = await this.$axios.get("orders", {
        params: this.queryInfo,
      });
      if (data.meta.status !== 200) {
        return this.$message.error(data.meta.msg);
      }
      this.orderList = data.data.goods;
      this.total = data.data.total;
      // console.log(data, this.orderList, this.total)
    },
    //分页功能
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize;
      this.getOrderList();
    },
    handleCurrentChange(newCurrent) {
      this.queryInfo.pagenum = newCurrent;
      this.getOrderList();
    },
    //打开编辑对话框
    showEditOrderDialog(info) {
      this.dialogOrderEdit = true;
      // console.log(info);
      this.orderEditData = info;
    },
    //修改订单状态确认按钮
    async editOrder() {
      const { data } = await this.$axios.put(
        "/orders/" + this.orderEditData.order_id,
        this.orderEditData
      );
      console.log(data);
      if (data.meta.status !== 201) {
        return this.$message.error(data.meta.msg);
      }
      this.$message.success(data.meta.msg);
      this.dialogOrderEdit = false;
      this.getOrderList();
    },
    editcancel() {
      this.dialogOrderEdit = false;
    },
    //展示订单详细
    async showOrderDetailed(id) {
      this.dialogOrderDetailed = true;
      const { data } = await this.$axios.get(`/orders/${id}`);
      // console.log(data);
      if (data.meta.status !== 200) {
        return this.$message.error(data.meta.msg);
      }
      this.orderDetailedData = data.data;
    },
  },
  mounted() {
    this.getOrderList();
  },
};
</script>

<style scoped></style>
